##
# Copyright (c) 2018-2022, NXOS Development Team
# SPDX-License-Identifier: Apache-2.0
# 
# Contains: Makefile for run PC32 Platform
# 
# Change Logs:
# Date           Author            Notes
# 2021-9-20      JasonHu           Init
##

#
# Config graphic window ? (y/n)
# 
GUI ?= n

#
# Config kernel vm ? (y/n)
# 
KVM ?= n

#
# Config hard disk ? (y/n)
# 
HD ?= n

#
# Config sound ? (y/n)
# 
SND ?= n

#
# Config nic ? (y/n)
# 
NIC ?= n

# netcard: tap0/user
NIC_TAP ?=n

# virtio config

#
# Config graphic window ? (y/n)
# 
VGUI ?= n

#
# Config hard disk ? (y/n)
# 
VHD ?= n

#
# Config sound ? (y/n)
# 
VSND ?= n

#
# Config nic ? (y/n)
# 
VNIC ?= n

#
# Tools for PC32
# 
TOOL_DIR 	:= tools/
GRUB_DIR	:= $(TOOL_DIR)grub-2.04
BIOS_FW_DIR	:= $(TOOL_DIR)bios_fw
RM			:= rm

#
# Tools name
# 
MAKE		:= make
QEMU 		:= qemu-system-i386
DEBUGER		:= $(CROSS_COMPILE)gdb
DUMP		:= $(CROSS_COMPILE)objdump

QEMU_KVM := # no virutal

HD0			:= ../../../rootfs.img

ifeq ($(KVM),y)
ifeq ($(HOSTOS),windows)
QEMU_KVM := -accel hax
else
QEMU_KVM := -enable-kvm
endif
endif

#
# Qemu args configure
# 
QEMU_ARGS 	:=  -m 512m $(QEMU_KVM) \
				-name "NXOS - Plartform PC32 - Arch x86" \
				-rtc base=localtime \
				-boot d \
				-cdrom $(NXOS_NAME).iso

ifeq ($(HD), y)
QEMU_ARGS 	+=	-drive id=disk0,file=$(HD0),format=raw,if=none \
				-device ahci,id=ahci \
				-device ide-hd,drive=disk0,bus=ahci.0
endif

ifeq ($(VHD), y)
QEMU_ARGS += -drive file=$(HD0),if=none,format=raw,id=x0 -device virtio-blk-pci,drive=x0,disable-legacy=on
endif


ifeq ($(GUI),y)
	QEMU_ARGS += -serial stdio
else

ifeq ($(VGUI),y)
	ifeq ($(shell uname),Darwin)
		QEMU_ARGS += -display default -vga virtio
	else
		QEMU_ARGS += -display sdl -vga virtio
	endif
else
	QEMU_ARGS += -nographic
endif

endif

ifeq ($(G),y)
	QEMU_ARGS += -S -gdb tcp::10001,ipv4
endif

ifeq ($(SND),y)
	QEMU_ARGS += -device sb16
endif

ifeq ($(NIC),y)
	QEMU_ARGS += -net nic,model=pcnet

ifeq ($(NIC_TAP),y)
	QEMU_ARGS += -net tap,ifname=tap0,script=no,downscript=no 
else
	QEMU_ARGS += -net user
endif

else

ifeq ($(VNIC),y)
	QEMU_ARGS += -netdev tap,ifname=tap0,id=mynet0 -device virtio-net-pci,netdev=mynet0,disable-legacy=on
endif

endif

#
# Args for make
#
.PHONY: run dryrun clean

#
# Run OS in QEMU
# 
run:
	@$(MAKE) -s -C $(GRUB_DIR) KERNEL=$(NXOS_NAME).elf OS_NAME=$(NXOS_NAME)
	$(QEMU) $(QEMU_ARGS)

#
# Dry Run OS in QEMU
# 
dryrun:
	$(QEMU) $(QEMU_ARGS)
# 
# Clear target file
# 
clean:
	@$(MAKE) -s -C $(GRUB_DIR) clean
	-$(RM) $(NXOS_NAME).elf

# 
# gdb debug
# 
gdb:
	@echo gdb load file from $(ARCH)/$(PLATFORM)/$(NXOS_NAME).elf
	$(DEBUGER) $(NXOS_NAME).elf -x connect.gdb
	
#
# dump kernel
#
dump:
	@echo dump kernel $(ARCH)/$(PLATFORM)/$(NXOS_NAME).elf
	$(DUMP) -D -S $(NXOS_NAME).elf > $(NXOS_NAME).dump.S
	